load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
load("//tools:defaults.bzl", "jasmine_test", "ts_project")

package(default_visibility = ["//visibility:public"])

STATIC_ASSETS = [
    "schema.json",
]

ts_project(
    name = "m3_theme_lib",
    srcs = glob(
        ["**/*.ts"],
        exclude = ["**/*.spec.ts"] + ["rules/components/test-setup-helper.ts"],
    ),
    tsconfig = "//src/material/schematics:tsconfig",
    deps = [
        "//:node_modules/@angular-devkit/schematics",
        "//:node_modules/@material/material-color-utilities",
        "//:node_modules/@types/node",
        "//:node_modules/sass",
        "//:node_modules/typescript",
        "//src/material/schematics:node_modules/@angular/cdk/schematics",
    ],
)

esbuild(
    name = "index_bundled",
    entry_point = ":index.ts",
    external = [
        "@angular/cdk/schematics",
        "@angular/material-experimental/",
        "@angular-devkit/schematics",
        "@angular-devkit/core",
        "typescript",
    ],
    format = "cjs",
    output = "index_bundled.js",
    platform = "node",
    target = "es2015",
    deps = [
        ":m3_theme_lib",
    ],
)

filegroup(
    name = "npm_package_assets",
    srcs = STATIC_ASSETS + [":index_bundled"],
)

########################################
#   Testing configuration              #
########################################

ts_project(
    name = "unit_tests_lib",
    testonly = True,
    srcs = glob([
        "**/*.spec.ts",
    ]) + [
        "index_bundled.d.ts",
    ],
    data = [":index_bundled"],
    tsconfig = "//src/material/schematics:tsconfig-test",
    deps = [
        ":m3_theme_lib",
        "//:node_modules/@angular-devkit/schematics",
        "//:node_modules/@bazel/runfiles",
        "//:node_modules/@material/material-color-utilities",
        "//:node_modules/sass",
        "//src/cdk/schematics/testing",
        "//tools/sass:sass_lib",
    ],
)

copy_to_bin(
    name = "unit_tests_assets",
    testonly = True,
    srcs = STATIC_ASSETS,
)

jasmine_test(
    name = "unit_tests",
    data = [
        ":unit_tests_assets",
        ":unit_tests_lib",
        "//src/material:sass_lib",
        "//src/material-experimental:sass_lib",
        "//src/material/schematics:collection_assets",
        "//src/material/schematics:ng_generate_assets",
        "//src/material/schematics:package_json",
    ],
    no_copy_to_bin = [
        # TODO(devversion): Ensure Sass is copied to `bazel-bin`.
        "//src/material:sass_lib",
        "//src/material-experimental:sass_lib",
    ],
    shard_count = 10,
)
